package cn.gudian.cms.Message

import cn.gudian.cms.sms.SmsClient

class SmsController {

  def index = {  redirect(action: "send", params: params)}
  def link = {
    params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
    def py = params?.py
    def linkManInstanceList
    def linkManInstanceTotal
    if (!py || py == "all") {
      linkManInstanceList = LinkMan.list(params)
      linkManInstanceTotal = LinkMan.count()
    } else {
      linkManInstanceList = LinkMan.findAllByPinyinLike("${py}%", params)
      linkManInstanceTotal = LinkMan.countByPinyinLike("${py}%")
    }

    [linkManInstanceList: linkManInstanceList, linkManInstanceTotal: linkManInstanceTotal]
  }
  def send = {
    []
  }
  def sendsave = {
    def smsRecordInstance = new SmsRecord()
    String[] mobiles = params?.mobiles.toString().split(",")

    int i = SmsClient.sendSMS(mobiles, params?.content)
    smsRecordInstance.mobile = params?.mobiles
    smsRecordInstance.content = params?.content
    smsRecordInstance.sendTime = new Date();
    smsRecordInstance.planTime = new Date();
    smsRecordInstance.state = i;
    if (!smsRecordInstance.hasErrors() && smsRecordInstance.save()) {
      flash.message = "提交成功，请到短信记录里面查询发送状态。"

      redirect(action: "send")
    }
    else {
      flash.message = "提交失败，请重试。"
      redirect(action: "send")
    }

  }

  def pressure = {
    if (params?.cardNum && params?.cardPassword) {

      int i = SmsClient.chargeUp(params?.cardNum, params?.cardPassword)
      if (SmsClient.returnInfo(i) == 0) {
        flash.message = "充值成功"
        redirect(action: "pressure")
      } else {
        flash.message = SmsClient.returnInfo(i)
        redirect(action: "pressure")
      }

    }
  }
  def record = {
    []
  }
}
